package com.company;

import javax.swing.*;
import java.awt.*;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.awt.event.WindowAdapter;  
import java.awt.event.WindowEvent;  
import java.util.Timer;  
import java.util.TimerTask;

/**
 * Desc: 显示全屏弹窗
 * @author jnwangap
 * @date 2024/3/15 9:54
 */
public class ReminderWindow extends JFrame {  
  
    private JLabel reminderLabel;  
    private Timer timer;  
  
    public ReminderWindow(String message) {  
        super("Reminder");  
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setUndecorated(true);  
        this.setBackground(new Color(0, 0, 0, 0));  
        this.setSize(Toolkit.getDefaultToolkit().getScreenSize());  
        this.setLocationRelativeTo(null);  
  
        reminderLabel = new JLabel(message);  
        reminderLabel.setHorizontalAlignment(JLabel.CENTER);  
        reminderLabel.setVerticalAlignment(JLabel.CENTER);  
        reminderLabel.setForeground(Color.WHITE);  
        reminderLabel.setFont(new Font("Dialog", Font.BOLD, 48));  
  
        this.add(reminderLabel);  
        this.setVisible(true);  
  
        // 添加关闭窗口的监听器  
        this.addWindowListener(new WindowAdapter() {  
            @Override  
            public void windowClosing(WindowEvent e) {  
                dispose();  
                if (timer != null) {  
                    timer.cancel();  
                }  
            }  
        });  
    }  
  
    public void showAfterDelay(long delayMillis) {  
        timer = new Timer();  
        timer.schedule(new TimerTask() {  
            @Override  
            public void run() {  
                SwingUtilities.invokeLater(new Runnable() {  
                    @Override  
                    public void run() {  
                        ReminderWindow.this.setVisible(true);  
                    }  
                });  
            }  
        }, delayMillis);  
    }  
  
    public void closeWindow() {  
        SwingUtilities.invokeLater(new Runnable() {  
            @Override  
            public void run() {  
                ReminderWindow.this.dispose();  
            }  
        });  
    }  
}